﻿/// <reference path="jquery-1.4.2-vsdoc.js" />
var orderby = "";
$(document).ready(function() {
    InitData(1); //首次加载数据

    $("#page_theme").change(function() {
        $("#Pagination").attr('class', $(this).val());
    });

    //全选
    $("#btnSelect").click(function() {
        var thisSelected = $(this).val() == "全 选";
        $("#Table tr:gt(0) input[type='checkbox']").attr("checked", thisSelected);
        $("#Table tr:gt(0)")[thisSelected ? "addClass" : "removeClass"]("selected");
        $(this).val([thisSelected ? "反 选" : "全 选"]);
        $(this).attr("title", [thisSelected ? "反 选" : "全 选"]);
    });

    //日历
    $("#search input[type='text']:gt(1)").click(function() {
        WdatePicker();
    });

    //添加
    $("#btnAdd").click(function() {
        Add();
    });

    //删除之后总是得到第一页数据
    $("#btnDelete").click(function() {
        var Select = $("#Table tr:gt(0) input[type='checkbox']:checked");
        DeleteSelect(Select);
    });
});

function InitData(page_index) {
    var tbody = "";
    $.ajax({
        type: "post",
        dataType: "json",
        url: "ashx/jsonPager.ashx",
        data: "type=json&page=" + page_index + "&orderby=" + orderby,
        beforeSend: function() { $("#div_load").show(); $("#Pagination").hide(); },
        complete: function() { $("#div_load").hide(); $("#Pagination").show(); },
        success: function(json) {
            $("#Table tr:gt(0)").remove();
            var linkData = json.Table;
            $.each(linkData, function(i, n) {
                var trs = "";
                trs += "<tr><td><input id='" + n.linkID + "' type='checkbox' /></td><td>" + n.linkID + "</td><td>" + n.linkSort + "</td><td>" + n.linkName + "</td><td><img src=images/" + n.linkDisplay + ".png /></td><td>" + n.linkUrl + "</td><td>" + n.linkDesc + "</td><td>" + n.linkAddDate + "</td><td><a id='look" + n.linkID + "' name='look' title='查看' class='abutton' href='#look'>查看</a><a id='edit" + n.linkID + "' name='edit' title='编辑' class='abutton' href='#edit'>编辑</a><a id='delete" + n.linkID + "' name='delete' title='删除' class='abutton1' href='#delete'>删除</a></td></tr>";
                tbody += trs;
            });
            $("#Table").append(tbody);
            var thisTr = $("#Table tr:gt(0)");
            thisTr.addClass("oddRow");
            //行单击事件
            thisTr.find("td:lt(8)").click(function() {
                var hasSelected = $(this).parent().hasClass("selected");
                $(this).parent()[hasSelected ? "removeClass" : "addClass"]("selected").find(":checkbox").attr("checked", !hasSelected);
            });

            $("#Table tr:gt(0)").hover(function() { $(this).addClass("mouseover"); }, function() { $(this).removeClass("mouseover") });
            $("#Table tr").each(function() {
                $(this).children("td:first").addClass("width60");
            });

            $("#Table tr:gt(0)").each(function() {
                $(this).children("td:last").find("a").click(function() {
                    var flag = $(this).attr("name");
                    var id = $(this).parent().parent().find("input").attr("id");
                    if ("look" == flag) {//查看
                        Look(id);
                    } else if ("edit" == flag) {//编辑
                        Edit(id);
                    } else if ("delete" == flag) {//Ajax删除
                        Delete(id);
                    }
                });
            });
        }
    });
    $.ajax({
        type: "post",
        dataType: "text",
        url: "ashx/jsonPager.ashx",
        data: "type=pager",
        beforeSend: function() { $("#Pagination").html("正在加载中……"); },
        success: function(data) {
            $("#Pagination").html("");
            $("#Pagination").append(data);
            //分页
            $("#Pagination a").each(function() {
                $(this).click(function() {
                    var href = $(this).attr("href");
                    InitData(href.substring(1, href.length));
                });
            });
        }
    });
}

//添加
function Add() {
    $.jBox("iframe:friendlink_edit.aspx?flag=add", {
        title: "友情连接新增",
        width: 500,
        height: 400,
        buttons: { '关闭': true },
        closed: function() { /*window.location = "jsonPager.html";*/InitData(1); }
    });
}

//删除
function Delete(id) {
    var submit = function(v, h, f) {
        if (v == 'ok') {
            $.jBox.tip("正在删除数据……", "loading");
            $.get("ashx/jsonPager.ashx",
                    { type: "delete", linkID: id },
                    function(data) {
                        if (data == "success") {
                            $.jBox.tip('删除成功', 'success');
                            InitData(1);
                        } else {
                            $.jBox.tip('删除失败', 'error');
                        }
                    });
        } else if (v == 'cancel') {
            $.jBox.tip('已取消', 'info');
        }
        return true; //close
    };
    $.jBox.confirm("是否删除（" + $("#" + id).parent().parent().find("td").eq(3).html() + "）？", "提示", submit);
}

function DeleteSelect(obj) {
    if (obj.length == 0) {
        $.jBox.info("没有选择数据！", "提示");
    } else {
        var submit = function(v, h, f) {
            if (v == 'ok') {
                $.jBox.tip("正在删除数据……", "loading");
                var selectList = "";
                obj.each(function() {
                    selectList += "-" + $(this).attr("id");
                });
                $.get("ashx/jsonPager.ashx",
                    { type: "deleteSelect", linkList: selectList },
                    function(data) {
                        if (data == "success") {
                            $.jBox.tip('删除成功', 'success');
                            InitData(1);
                        } else {
                            $.jBox.tip('删除失败', 'error');
                        }
                    });
            } else if (v == 'cancel') {
                $.jBox.tip('已取消', 'info');
            }
            return true; //close
        };
        $.jBox.confirm("选择了" + obj.length + "条记录，是否删除？", "提示", submit);
    }
}


//编辑
function Edit(id) {
    $.jBox("iframe:friendlink_edit.aspx?flag=edit&linkID=" + id, {
        title: "友情连接编辑",
        width: 500,
        height: 400,
        buttons: { '关闭': true },
        closed: function() { InitData(1); }
    });
}

function Look(id) {
    $.jBox("iframe:friendlink_edit.aspx?flag=look&linkID=" + id, {
        title: "友情连接查看",
        width: 500,
        height: 400,
        buttons: { '关闭': true }
    });
}